﻿using UnityEngine;
using System.Collections;

public class PlayerMove : MonoBehaviour {

    public float forceMove = 100;

    public float jumpVelocity = 100;

    private Animator anim;

    private bool isGround = false;

    private bool isWall = false;

    private Transform wallTrans = null;

    private bool isSlider = false;

    void Awake() {
        anim = this.GetComponent<Animator>();
    }

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
        float h = Input.GetAxis("Horizontal");

        if (isSlider == false)
        {
            //左右移动
            if (h > 0.05)
            {
                rigidbody2D.AddForce(Vector2.right * this.forceMove);
            }
            else if (h < -0.05)
            {
                rigidbody2D.AddForce(-Vector2.right * this.forceMove);
            }

            //转向
            this.Turn(h);
        }

        //控制跳跃
        if (Input.GetKeyDown(KeyCode.Space))
        {
            PlayManager.Instance.PlayPlayerJump();
            Vector2 velocity = rigidbody2D.velocity;
            velocity.y = jumpVelocity;
            rigidbody2D.velocity = velocity;
        }

        //设置动画
        anim.SetFloat("horizontal", Mathf.Abs(h));
        anim.SetFloat("vertical", rigidbody2D.velocity.y);
        anim.SetBool("isGround", isGround);
        anim.SetBool("isWall", isWall);

        if (isWall == false || isGround == true)
        {
            isSlider = false;
        }
    }

    /// <summary>
    /// 控制主角转向
    /// </summary>
    private void Turn(float h) {        
        if (h > 0.05)
        {
            transform.localScale = new Vector3(1, 1, 1);
        }
        else if (h < -0.05)
        {
            transform.localScale = new Vector3(-1, 1, 1);
        }
    }

    /// <summary>
    /// 发生碰撞
    /// </summary>
    /// <param name="coll"></param>
    public void OnCollisionEnter2D(Collision2D coll)
    {
        if (coll.transform.tag.Equals("Ground"))
        {
            isGround = true;
        }
        else if (coll.transform.tag.Equals("Wall"))
        {
            isWall = true;
            rigidbody2D.velocity = Vector2.zero;
            rigidbody2D.gravityScale = 10;
            wallTrans = coll.transform;
        }
    }

    /// <summary>
    /// 结束碰撞
    /// </summary>
    /// <param name="coll"></param>
    public void OnCollisionExit2D(Collision2D coll)
    {
        if (coll.transform.tag.Equals("Ground"))
        {
            isGround = false;            
        }
        else if (coll.transform.tag.Equals("Wall"))
        {
            isWall = false;
            rigidbody2D.gravityScale = 30;
        }
    }

    /// <summary>
    /// slider状态时调整角色的方向
    /// </summary>
    public void ChangeDir()
    {
        isSlider = true;
        if (wallTrans.position.x > transform.position.x)
        {
            transform.localScale = new Vector3(-1, 1, 1);
        }
        else if (wallTrans.position.x < transform.position.x)
        {
            transform.localScale = new Vector3(1, 1, 1);
        }
    }
}
